package com.bobo.mm.dogs;

import android.annotation.TargetApi;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.bobo.mm.socket.SocketService;

import static android.content.ContentValues.TAG;

/**
 * 看门狗之父
 * Created by cocolove2 on 2020/6/18.
 */
public abstract class DogFather {
    /**
     * 心跳计数器
     */
    private int pingCount;
    /**
     * 最大忍受次数
     */
    private int maxCountEndure;
    private Context mContext;
    private JobScheduler mJobScheduler;
    private DogListener mDogListener;

    public int getPingCount() {
        return pingCount;
    }

    public void setPingCount(int pingCount) {
        this.pingCount = pingCount;
    }

    public int getMaxCountEndure() {
        return maxCountEndure;
    }

    public void setMaxCountEndure(int maxCountEndure) {
        this.maxCountEndure = maxCountEndure;
    }

    public DogFather(Context context) {
        this.mContext = context;
    }


    /**
     * 看门狗跑去看门，觅食
     */
    public void doForaging() {
        if (pingCount >= maxCountEndure) {
            howlToService(mContext);
        }
        pingCount++;
        if (mDogListener != null) {
            mDogListener.lookup(pingCount, maxCountEndure);
        }
    }

    /**
     * 看门狗吃到食物回窝
     */
    public void happy() {
        pingCount = 0;
    }

    /**
     * 看门狗下班回窝睡觉,不再工作
     */
    public void goHome() {
        pingCount = 0;
        closeDogJob(mContext);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    protected void buildDogJob(int duration) {
        if (mJobScheduler != null) {
            mJobScheduler.cancel(getDogId());
        }
        duration = duration <= 0 ? 30000 : duration;
        JobInfo.Builder builder = new JobInfo.Builder(getDogId(), new ComponentName(mContext.getApplicationContext(), DogJobService.class));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.setMinimumLatency(duration);
        } else {
            builder.setPeriodic(duration);
        }
        mJobScheduler = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        if (mJobScheduler != null && mJobScheduler.schedule(builder.build()) == JobScheduler.RESULT_SUCCESS) {
            Log.d(TAG, getClass().getName() + "#编号:" + getDogId() + "的任务创建成功");
        } else {
            Log.e(TAG, getClass().getName() + "#编号:" + getDogId() + "的任务创建失败");
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void closeDogJob(Context context) {
        JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        if (scheduler != null) {
            scheduler.cancel(getDogId());
            Log.d(TAG, getClass().getName() + "#编号:" + getDogId() + "的任务取消成功！");
        }
    }

    protected abstract int getDogId();

    /**
     * 看门狗向服务狂吠，索取食物
     */
    private void howlToService(Context context) {
        Intent daemonIntent = new Intent();
        daemonIntent.setPackage(context.getPackageName());
        daemonIntent.setClass(context, SocketService.class);
        context.startService(daemonIntent);
    }

    public interface DogListener {
        void lookup(int count, int maxCount);
    }

    public void setDogListener(DogListener dogListener) {
        mDogListener = dogListener;
    }
}
